package com.wang.luntan.users.head.controller;

import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.pojo.ApiResponseEnum;
import com.wang.luntan.common.util.ConstatFinalUtil;
import com.wang.luntan.common.web.controller.BaseController;
import com.wang.luntan.users.pojo.AtUsers;
import com.wang.luntan.users.service.IUsersDbService;
import com.wang.luntan.users.service.IUsersOperService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户首页操作
 *
 * @author wangsh
 */
@Log4j2
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController {
    @Resource
    private IUsersOperService usersOperService;
    @Resource
    private IUsersDbService usersDbService;
    
    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpServletRequest request) {
        log.info("--main--");
//        return "/head/users/main";
        return "redirect:/head/users/usersUpdate";
    }

    /**
     * 打开用户更新
     *
     * @return
     */
    @RequestMapping("/usersUpdate")
    public String usersUpdate(HttpServletRequest request, Model model) {
        log.info("--usersUpdate--");
        String operType = request.getParameter("operType");
        /* 从session中获取信息 */
        AtUsers users = (AtUsers) this.findObjfromSession(request, "users");
        /* 准备请求的信息 */
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("id", users.getId() + "");
        ApiResponse<AtUsers> apiResponse = this.usersOperService.usersUpdate(paramsMap);
        users = apiResponse.getDataOneJava();
        model.addAttribute("response", apiResponse.toJSON());
        if ("updateInfo".equalsIgnoreCase(operType)) {
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdateInfo";
        } else if ("updatePass".equalsIgnoreCase(operType)) {
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdatePass";
        } else if ("updatePhoto".equalsIgnoreCase(operType)) {
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdatePhoto";
        }
        return "/head/users/usersInfo";
    }

    /**
     * 打开用户提交
     *
     * @return
     */
    @RequestMapping("/usersUpdateSubmit")
    public String usersUpdateSubmit(HttpServletRequest request, Model model) {
        log.info("--usersUpdateSubmit--");
        String operType = request.getParameter("operType");
        /* 从session中获取信息 */
        AtUsers users = (AtUsers) this.findObjfromSession(request, "users");

        /* 准备请求的信息 */
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("id", users.getId() + "");
        paramsMap.put("operType", operType);

        if ("updateInfo".equalsIgnoreCase(operType)) {
            /* 用户填写的信息 */
            String nickName = request.getParameter("nickName");
            String trueName = request.getParameter("trueName");
            String birthdayStr = request.getParameter("birthdayStr");
            String address = request.getParameter("address");
            String qq = request.getParameter("qq");
            String phone = request.getParameter("phone");
            String regionId = request.getParameter("regionId");
            String sex = request.getParameter("sex");
            String weiXin = request.getParameter("weiXin");
            String education = request.getParameter("education");

            paramsMap.put("nickName", nickName);
            paramsMap.put("trueName", trueName);
            paramsMap.put("birthdayStr", birthdayStr);
            paramsMap.put("address", address);
            paramsMap.put("qq", qq);
            paramsMap.put("phone", phone);
            paramsMap.put("regionId", regionId);
            paramsMap.put("sex", sex);
            paramsMap.put("weiXin", weiXin);
            paramsMap.put("education", education);

            ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
            model.addAttribute("response", apiResponse.toJSON());
            /* 根据不同的请求跳转不同的页面 */
            request.getSession().setAttribute("users", users);
            return "/head/users/usersUpdateInfo";
        } else if ("updatePass".equalsIgnoreCase(operType)) {
            String souPass = request.getParameter("souPass");
            String password = request.getParameter("password");

            paramsMap.put("souPass", souPass);
            paramsMap.put("password", password);

            ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
            model.addAttribute("response", apiResponse.toJSON());
            request.getSession().setAttribute("users", users);
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdatePass";
        } else if ("updateEmail".equalsIgnoreCase(operType)) {
            String email = request.getParameter("email");

            paramsMap.put("email", email);

            ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
            model.addAttribute("response", apiResponse.toJSON());
            request.getSession().setAttribute("users", users);
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdateEmail";
        } else if ("updatePhoto".equalsIgnoreCase(operType)) {
            /* 上传头像 */
            try {
                /* 更新标志 */
                if (request instanceof MultipartRequest) {
                    MultipartRequest multipartRequest = (MultipartRequest) request;
                    MultipartFile multipartFile = multipartRequest.getFile("file");
                    paramsMap.put("fileName", multipartFile.getOriginalFilename());
                    paramsMap.put("inputStream", multipartFile.getInputStream());
                    users.setUpdateTime(new Date());
                    ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
                    model.addAttribute("response", apiResponse.toJSON());
                    request.getSession().setAttribute("users", users);
                }
            } catch (Exception e) {
                log.error("更新图片失败了;", e);
            }
            /* 根据不同的请求跳转不同的页面 */
            return "/head/users/usersUpdatePhoto";
        } else {
            ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
            model.addAttribute("response", apiResponse.toJSON());
        }
        return "/head/users/usersInfo";
    }

    /**
     * 退出页面
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpSession session) {
        log.info("--logout--");
        String returnUrl = request.getParameter("returnUrl");
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 获取用户 */
        AtUsers users = (AtUsers) this.findObjfromSession(request, "users");
        if (returnUrl == null) {
            returnUrl = "";
        }
        try {
            returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
            /* 做一些清除的操作 */
            session.removeAttribute("users");
            session.removeAttribute("lastLoginTime");

            apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus());
        } catch (Exception e) {
            apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
            log.error("退出报错了;email:{}", users.getEmail(), e);
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        /* 提示信息 */
        session.setAttribute("response", apiResponse.toJSON());
        /* 客户端跳转到登陆页面 */
        return "redirect:/login?returnUrl=" + returnUrl;
    }
}
